package bootstrap

import (
	"time"

	customLogger "gitee.com/sweetsunnyflower/carpooling-server-go/pkg/logger"
	"gitee.com/sweetsunnyflower/carpooling-server-go/routes"
	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/middleware/logger"
	"github.com/gofiber/fiber/v2/middleware/recover"
	"github.com/gofiber/fiber/v2/middleware/requestid"
)

func SetUpRouter() {
	// 注册全局中间件
	registerGlobalMiddlewares(App)

	// 静态资源
	routes.RegisterStaticService(App)

	// 管理后台路由
	routes.RegisterAdminRoutes(App)

	// api路由
	routes.RegisterApiRoutes(App)

	// websocket路由
	routes.RegisterWebsocketRoutes(App)
}

func registerGlobalMiddlewares(app *fiber.App) {
	app.Use(
		requestid.New(),
		logger.New(logger.Config{
			Format:        `{"time":"${time}", "ip":"${ip}", "host":"${host}", "referer":"${referer}", "ua":"${ua}", "requestid":"${respHeader:X-Request-ID}","status":${status},"latency":"${latency}","method":"${method}","path":"${path}","query":"${queryParams}", "body":${body},"response":${resBody}}` + "\n",
			TimeFormat:    "2006-01-02 15:04:05",
			TimeZone:      "Local",
			TimeInterval:  500 * time.Millisecond,
			Output:        customLogger.Writer,
			DisableColors: false,
		}),
		recover.New(),
	)
}
